%{
 open Ast;;
 let parse_error s = (* Called by the parser function on error *)
 print_endline  s;
 flush stdout
%}

%token <int> INT
%token PLUS MINUS MUL DIV
%token MATCH WITH PIPE ARROW NEW_LINE LPAREN RPAREN
%token <string> IDENT
%token FUN IDENT FUN_ARROW EOF

%left PLUS MINUS
%left MUL DIV
%left FUN IDENT FUN_ARROW
%left MATCH
%left LPAREN

%start main
%type <Ast.appexp> main
%type <Ast.exp> exp

%%
exp: INT                       { IntConst($1)}
| IDENT                        { StringConst($1)}
| exp PLUS exp                 { Binop( Plus, $1, $3)}
| exp MINUS exp                { Binop( Minus,$1, $3)}
| exp MUL exp                  { Binop( Mul, $1, $3)}
| exp DIV exp                  { Binop( Div, $1, $3)}
| LPAREN exp RPAREN            { $2 }
| FUN IDENT FUN_ARROW exp      { Fun( $2,$4)}
| MATCH exp WITH match_exp     { Match($2,$4)}
;


match_exp: { []}
| PIPE IDENT ARROW exp  match_exp {($2,$4)::$5}
/*| match_exp                             { $1}*/

;

appexp : exp                   { $1} 
| appexp exp                   { App($1,$2)}
;

main : appexp EOF              { $1}
%%
